from robyn.exceptions import HTTPException
from robyn import Robyn, Request, Response, jsonify
import traceback
import logging

from common.result import JsonResult
from common.response.http_response import CustomBaseResponse
from common.response.response_code import StandardResponseCode

logger = logging.getLogger(__name__)

def register_exception(app: Robyn):
    ...
    
def default_exception_handler(exc: Exception):
    """异常处理

    Args:
        request (Request):
        exc (HTTPException):
    """
    '''打印堆栈信息'''
    tb = traceback.format_exc()
    logger.error(tb)
    if isinstance(exc, HTTPException):
        '''HTTP Exception handler'''
        return JsonResult.fail(code=exc.status_code, msg=exc.detail)
    else:
        '''Default Exception'''
        return JsonResult.fail(code=StandardResponseCode.HTTP_500, msg="服务端异常")